package com.ruoyi.individual.controller;

import com.ruoyi.book.domain.Book;
import com.ruoyi.book.domain.BorrowInfo;
import com.ruoyi.book.mapper.BorrowMapper;
import com.ruoyi.book.service.IBorrowService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.individual.domain.BorrowBook;
import com.ruoyi.individual.service.IMyBorrowService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的借阅Controller
 *
 * @author yaohui
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/individual/myBorrow")
public class MyBorrowController extends BaseController {

    @Autowired
    private IMyBorrowService myBorrowService;
    @Autowired
    private IBorrowService borrowService;
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询我的借阅列表
     */
//    @PreAuthorize("@ss.hasPermi('individual:myBorrow:list')")
    @GetMapping("/list")
    public TableDataInfo list(BorrowBook borrowBook) {
        startPage();
        // 我的借阅书本列表详细信息
//        List<BorrowBook> borrowBooks = myBorrowService.selectMyBorrowBookInfoList(borrowBook, getUsername());
        List<BorrowBook> borrowBooks = myBorrowService.selectMyBorrowBookInfoList(borrowBook, "admin");
        return getDataTable(borrowBooks);
    }

    /**
     * 导出我的借阅列表
     */
//    @PreAuthorize("@ss.hasPermi('individual:myBorrow:export')")
    @Log(title = "我的借阅", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
//        List<BorrowBook> list =myBorrowService.selectMyBorrowBookInfoList(null, getUsername());
        List<BorrowBook> list =myBorrowService.selectMyBorrowBookInfoList(null, "admin");
        ExcelUtil<BorrowBook> util = new ExcelUtil<BorrowBook>(BorrowBook.class);
        util.exportExcel(response, list, "我的借阅数据");
    }

    /**
     * 获取我的借阅详细信息
     */
//    @PreAuthorize("@ss.hasPermi('individual:myBorrow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(myBorrowService.selectMyBorrowBookInfoById(id, getUsername()));
    }
}
